package com.zd.web.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zd.entity.Novel;
import com.zd.entity.User;
import com.zd.service.UserCenterService;
import com.zd.service.impl.UserCenterServiceImpl;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserCenterServlet")
public class UserCenterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//实例化UserService接口下的UserServiceImpl
		UserCenterService ucs = new UserCenterServiceImpl();
		
		//获取user数据
		User user = (User)request.getSession().getAttribute("user");
		if(user==null) {
			response.sendRedirect("login.jsp");
			return;
		}
		System.err.println(user);
		//通过UserServiceImpl查询收藏书籍和最近阅读
		List<Novel> cl = ucs.collectionBook(user);//收藏
		List<Novel> rl = ucs.recentBook(user);//最近
		
		//将收藏书籍和最近阅读上传到session
		request.getSession().setAttribute("cl", cl);
		request.getSession().setAttribute("rl", rl);
		//转发到个人中心页面
		request.getRequestDispatcher("person.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
